<template>
    <div class="creation-card " v-for="(item, index) in creationList" :key="index">
        <div class="creation-title" @click="handleDetails(item)">
            {{ item.title }}
        </div>
        <div class="creation-content" @click="handleDetails(item)">
            <div class="creation-cover" v-if="item.coverUrl !== null && item.cover !== ''">
                <img :src="item.coverUrl" alt="暂时无法加载">
            </div>

            <div class="creation-description" :style="{ width: handleResize(item) }">
                <p>
                    {{ getText(item.content) }}
                </p>
            </div>
        </div>
        <div class="creation-footer">
            <!-- 赞同次数 -->
            <div class="like"
                :style="{ background: item.like ? 'rgba(23, 114, 246)' : 'rgba(23, 114, 246, .1)', color: item.like ? '#fff' : '#1772f6' }">
                <div v-if="item.like" @click="handleCancelLike(item)">
                    <el-icon>
                        <CaretTop />
                    </el-icon>
                    已赞同
                    <span>{{ item.likeCount }}</span>
                </div>
                <div v-else @click="handleLike(item)">
                    <el-icon>
                        <CaretTop />
                    </el-icon>
                    赞同
                    <span>{{ item.likeCount }}</span>
                </div>
            </div>
            <!-- 踩 -->
            <div class="unlike" @click="handleUnLike(item)">
                <el-icon>
                    <CaretBottom />
                </el-icon>
                <span>{{ item.unLikeCount }}</span>
            </div>
            <div class="data">
                <!-- 浏览次数 -->
                <div class="data-item" style="display: flex;align-items: center;">
                    <el-icon>
                        <View />
                    </el-icon>
                    <span> &nbsp;{{ item.viewCount }}&nbsp;</span>
                    次浏览
                </div>
                <!-- 讨论次数 -->
                <div slot="查看详细" class="data-item" style="display: flex;align-items: center;">
                    <el-icon>
                        <ChatDotSquare />
                    </el-icon>
                    <span>&nbsp; {{ item.commentCount }}&nbsp;</span>次讨论
                </div>
            </div>
            <!-- 分享 -->
            <div class="share">
                <el-icon>
                    <Promotion />
                </el-icon>
                分享
            </div>
            <!-- 收藏 -->
            <div class="collect">
                <div v-if="!item.favorite" @click="handleFavorite(item)">
                    <el-icon>
                        <Star />
                    </el-icon>
                    <span>收藏</span>
                </div>
                <div v-else style="color: #a07f42;" @click="handleCancelFavorite(item)">
                    <el-icon>
                        <StarFilled />
                    </el-icon>
                    已收藏
                </div>
            </div>
            <!-- 查看详情 -->
            <div class="detail" @click="handleDetails(item)">
                <el-icon>
                    <MoreFilled />
                </el-icon>
                阅读全文
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { defineProps, ref } from 'vue'
import type { PropType } from 'vue'
import { updateCreation, operateCreation } from '@/api/creation'
import { useRouter } from 'vue-router'

const router = useRouter()

const props = defineProps({
    creationList: Array as PropType<any[]>,
    default: []
});

//动态获取宽度
const handleResize = (item: any) => {
    if (item.coverUrl == null || item.cover == '') {
        return '100%'
    }
    return '470px'
}

// 将html转化为纯文本
const getText = (html: any) => {
    const tempDiv = document.createElement("div");
    tempDiv.innerHTML = html;
    return tempDiv.innerText;
}

//点赞
const handleLike = (creation: any) => {
    creation.likeCount += 1
    creation.like = true
    operateCreation(creation.id, 'like', 'add', creation)
}

//取消点赞
const handleCancelLike = (creation: any) => {
    creation.likeCount -= 1
    creation.like = false
    operateCreation(creation.id, 'like', 'cancel', creation)
}

//踩
const handleUnLike = (creation: any) => {
    if (creation.like) {
        handleCancelLike(creation)
    }
}

//收藏
const handleFavorite = (creation: any) => {
    creation.favorite = !creation.favorite
    operateCreation(creation.id, 'favorite', 'add', creation)
}

//取消收藏
const handleCancelFavorite = (creation: any) => {
    creation.favorite = false
    operateCreation(creation.id, 'favorite', 'cancel', creation)
}

//查看详情
const handleDetails = (item: any) => {
    item.viewCount += 1
    updateCreation(item)
    router.push({
        path: 'details-article_question',
        query: { id: item.id }
    })
}
</script>

<style scoped>
.creation-card {
    text-align: left;
    display: flex;
    flex-wrap: wrap;
    cursor: pointer;
    margin-bottom: 25px;
    font-size: 15px !important;

    .creation-title {
        width: 100%;
        height: 25px;
        font-size: 18px;
        font-weight: bold;
        line-height: 25px;
        text-align: left;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        transition: all 0.3s;
        cursor: pointer;
    }

    .creation-title:hover {
        color: #09408e;
    }


    .creation-content {
        width: 700px;
        margin-top: 5px;
        height: 120px;
        text-align: left;
        display: flex;
        cursor: pointer;
        font-size: 15px !important;

        .creation-cover {
            width: 200px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            border-radius: 5px;
            margin-right: 10px;

            img {
                width: 100%;
                height: auto;
            }
        }

        .creation-description {
            width: 470px;
            overflow: hidden;
            text-overflow: ellipsis;
            line-height: 30px;
            line-clamp: 4;
            -webkit-line-clamp: 4;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            transition: all 0.3s;
            font-size: 15px;
        }

        .creation-description:hover {
            color: #707379;
        }

        .creation-description-change {
            width: 450px;
        }

        .creation-description-change-p {
            width: 100%;
        }
    }

    .creation-footer {
        margin-top: 10px;
        height: 30px;
        display: flex;
        width: 100%;
        align-items: center;

        .like {
            background-color: rgba(23, 114, 246, .1);
            text-align: center;
            width: 120px;
            height: 30px;
            font-size: 14px;
            color: #ffffff;
            line-height: 30px;
            cursor: pointer;
            border-radius: 5px;
            margin-right: 10px;
        }

        .like:hover {
            background-color: rgba(23, 114, 246, .15);
            color: #1772f6;
        }

        .unlike {
            background-color: rgba(23, 114, 246, .1);
            text-align: center;
            width: 40px;
            height: 30px;
            font-size: 14px;
            color: #1772f6;
            line-height: 30px;
            cursor: pointer;
            border-radius: 5px;
            padding-left: 5px;
            padding-right: 5px;
        }

        .unlike:hover {
            background-color: rgba(23, 114, 246, .15);
            color: #1772f6;
        }

        .data {
            width: 250px;
            display: flex;
            margin-left: auto;

            .data-item {
                height: 30px;
                line-height: 30px;
                font-size: 13px;
                color: #75849a;
                cursor: pointer;
                width: 50%;
            }
        }

        .share {
            height: 30px;
            line-height: 30px;
            font-size: 13px;
            color: #75849a;
            cursor: pointer;
            margin-right: 25px;
            transition: all 0.5s;
        }

        .share:hover {
            color: #2f6932fd;
        }

        .collect {
            height: 30px;
            line-height: 30px;
            font-size: 13px;
            color: #75849a;
            cursor: pointer;
            transition: all 0.5s;
            margin-right: 25px;
        }

        .collect:hover {
            color: #a07f42;
        }

        .detail {
            height: 30px;
            line-height: 30px;
            font-size: 13px;
            color: #5a87cb;
            cursor: pointer;
            transition: all 0.5s;
        }

        .detail:hover {
            color: #1772f6;
        }
    }
}
</style>